#!/bin/bash

SUFFIX=facts

case $1 in
    --facts)
        SUFFIX=facts
        shift
        ;;
    --results)
        SUFFIX=results
        shift
        ;;
esac

if [ -n "$2" ]; then
    arg1=$(realpath "$1")
    arg2=$(realpath "$2")
else
    arg1="@CMAKE_SOURCE_DIR@/tools/ooanalyzer/tests/$1.$SUFFIX"
    arg2="@CMAKE_BINARY_DIR@/tools/ooanalyzer/tests/$1.$SUFFIX"
fi

for file in "$arg1" "$arg2"; do
    if [ ! -f "$file" ]; then
        echo "Cannot find file $file"
        exit 1
    fi
done

PROG="[comparefacts],terms_compare('$arg1', '$arg2')"
ERROR="writeln('Error running comparefacts'), halt(127)"

cd @CMAKE_CURRENT_SOURCE_DIR@
@SWIPL_EXECUTABLE@ -q -g "catch((($PROG);true), E, ($ERROR)), halt."
